 /**
This file is part of Studentport.

    Studentport is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Studentport is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Studentport.  If not, see <http://www.gnu.org/licenses/>.

**/


package com.httpclasses;

import java.io.IOException;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLReader {

	public static List<Vobject> getXML(String content) {

		List<Vobject> vobjects = new LinkedList<Vobject>();
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		// Using factory get an instance of document builder
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			StringReader reader = new StringReader(content);
			InputSource inputSource = new InputSource(reader);
			// InputSource inputSource = new InputSource(url.openStream() );
			// parse using builder to get DOM representation of the XML file
			Document dom;
			// dom = db.parse(inputSource);
			dom = db.parse(inputSource);
			Element docEle = dom.getDocumentElement();

			// get a nodelist of elements
			NodeList nl = docEle.getElementsByTagName("item");
			if (nl != null && nl.getLength() > 0) {
				for (int i = 0; i < nl.getLength(); i++) {

					// get the employee element
					Element el = (Element) nl.item(i);

					// get the Employee object
					Vobject e = getVobject(el);

					// add it to list
					vobjects.add(e);
				}
			}

			return vobjects;

		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;

	}

	/**
	 * I take a xml element and the tag name, look for the tag and get the text
	 * content i.e for <employee><name>John</name></employee> xml snippet if the
	 * Element points to employee node and tagName is 'name' I will return John
	 */
	private static String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if (nl != null && nl.getLength() > 0) {
			Element el = (Element) nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}

	/**
	 * Calls getTextValue and returns a int value
	 */
	private static int getIntValue(Element ele, String tagName) {
		// in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele, tagName));
	}

	private static Vobject getVobject(Element obj) {
		String line_number = obj.getAttribute("line_number");
		String traffic_island = obj.getAttribute("traffic_island");
		String next_trip = obj.getAttribute("next_trip");
		String next_next_trip = obj.getAttribute("next_next_trip");
		String dest = getTextValue(obj, "destination");
		String color = obj.getAttribute("line_number_background_color");

		Vobject v = new Vobject(dest, traffic_island, next_trip,
				next_next_trip, line_number, color);
		return v;

	}
}